2 Backtracking. Infinitely slow.
26 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
27 #define For(i, a, b) for (int i=(a); i<(b); ++i)
28 #define D(x) cout << #x " is " << x << endl
33 ones
, twos
, threes
, fours
, fives
, sixes
, chance
, three_of_a_kind
,
34 four_of_a_kind
, five_of_a_kind
, short_straight
, long_straight
,
38 int score(turn t
, int c
){
40 for (int i
=0; i
<6; ++i
) times
[t
[i
]]++;
55 return accumulate(t
, t
+5, 0);
57 for (int i
=1; i
<=6; ++i
) if (times
[i
] >= 3) return accumulate(t
, t
+5, 0);
60 for (int i
=1; i
<=6; ++i
) if (times
[i
] >= 4) return accumulate(t
, t
+5, 0);
63 for (int i
=1; i
<=6; ++i
) if (times
[i
] >= 5) return 50;
66 for (int i
=1; i
<=3; ++i
)
67 if (times
[i
] > 0 && times
[i
+1] > 0 && times
[i
+2] > 0 && times
[i
+3] > 0) return 25;
70 for (int i
=1; i
<=2; ++i
)
71 if (times
[i
] > 0 && times
[i
+1] > 0 && times
[i
+2] > 0 && times
[i
+3] > 0 && times
[i
+4] > 0) return 35;
75 for (int i
=1; i
<=6; ++i
)
76 for (int j
=1; j
<=6; ++j
)
77 if (times
[i
] == 3 && times
[j
] == 2) return 40;
80 printf("Something very bad happened.\n");
89 int best_match
[13]; //best_match[i] = Turn matched with category i
90 int current_match
[13];
92 void backtrack(int i
, int used_categories
, int current_score
){
95 for (int c
=ones
; c
<=sixes
; ++c
) sum
+= score(turns
[best_match
[c
]], c
);
96 int current_bonus
= 0;
98 current_score
+= (current_bonus
= 35); //bonus
100 printf("Found a matching of %d:\n", current_score
);
101 for (int c
=ones
; c
<=full_house
; ++c
){
102 printf("%d ", current_match
[c
]);
106 if (current_score
> best_score
){
107 best_score
= current_score
;
108 bonus
= current_bonus
;
109 memcpy(best_match
, current_match
, sizeof best_match
);
114 for (int c
=ones
; c
<=full_house
; ++c
){
115 if (!(used_categories
& (1 << c
))){
116 current_match
[c
] = i
;
117 backtrack(i
+ 1, used_categories
| (1 << c
), current_score
+ score(turns
[i
], c
));
118 current_match
[c
] = -1;
126 for (int i
=0; i
<13; ++i
){
127 for (int j
=0; j
<5; ++j
){
128 if (scanf("%d", &turns
[i
][j
]) != 1) return 0;
135 for (int c
=ones
; c
<=full_house
; ++c
){
136 printf("%d ", best_match
[c
]);
137 printf("%d %d\n", bonus
, best_score
);